package com.sy.controller.allocate;

import com.sy.bean.allocate.Allocate;
import com.sy.bean.allocate.AllocateQuery;
import com.sy.bean.pucharse.AuditStatus;
import com.sy.common.bean.PageBean;
import com.sy.common.bean.ResultBean;
import com.sy.common.controller.BaseController;
import com.sy.common.utils.BeanOfMap;
import com.sy.service.allocate.AllocateDetailService;
import com.sy.service.allocate.AllocateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@Api(tags = "仓库")
@RequestMapping("/api/allocate")
public class AllocateController extends BaseController {
    @Autowired
    AllocateService allocateService;
    @Autowired
    AllocateDetailService allocateDetailService;

    @ApiOperation("调拨列表")
    @PostMapping("/list")
    public ResultBean<PageBean<Allocate>> query(@RequestBody AllocateQuery allocateQuery) {
        PageBean<Allocate> pageBean = allocateService.query(BeanOfMap.bom(allocateQuery));
        return success("调拨列表", pageBean);
    }

    @ApiOperation("批量删除调拨单")
    @DeleteMapping("/batchDelete")
    public ResultBean batchDelete(@RequestParam("ids") List<Integer> ids) {
        int rs = allocateService.batchDelete(ids);
        return rs > 0 ? success("删除成功") : fail("删除失败");
    }

    @ApiOperation("批量删除调拨单明细")
    @DeleteMapping("/batchByDelete")
    public ResultBean batchByDelete(@RequestParam("ids") List<Integer> ids) {
        int rs = allocateDetailService.batchByDelete(ids);
        return rs > 0 ? success("删除成功") : fail("删除失败");
    }

    @ApiOperation("新增调拨单")
    @PostMapping("/save")
    public ResultBean save(@RequestBody @Validated Allocate allocate, BindingResult result) {
        validate(result);
        int rs = allocateService.save(allocate);
        return rs > 0 ? success("新增成功") : fail("新增失败");
    }

    @ApiOperation("导出调拨单")
    @PostMapping("/export")
    public ResultBean export(@RequestBody Map map) {
        return success("导出成功", allocateService.exportAll(map));
    }

    @ApiOperation("批量审核调拨单")
    @PutMapping("/audit")
    public ResultBean audit(@RequestBody AuditStatus auditStatus) {
        int rs = allocateService.batchStatus(auditStatus);
        return rs > 0 ? success("审核成功") : fail("审核失败");
    }
    @ApiOperation("编辑调拨单")
    @PutMapping("/edit")
    public ResultBean update(@RequestBody Allocate allocate) {
        int rs = allocateService.update(allocate);
        return gotoResult(rs,"修改");
    }
    @ApiOperation("调拨单详情")
    @GetMapping("/get")
    public ResultBean<Allocate> get(int id) {
        Allocate allocate = allocateService.get(id);
        return success("调拨详情",allocate);
    }
}
